package com.xuanmiao.smsback.web.controller.msg;

import com.panshi.hujin2.base.common.util.DozerUtils;
import com.panshi.hujin2.base.domain.result.BasicResult;
import com.panshi.hujin2.base.domain.result.BasicResultCode;
import com.panshi.hujin2.base.security.service.system.employee.EmployeeBO;
import com.panshi.hujin2.base.security.service.util.EmployeeUtil;
import com.xuanmiao.smsback.domain.exception.NoticeException;
import com.xuanmiao.smsback.domain.qo.BusinessNoticeQO;
import com.xuanmiao.smsback.service.notice.INoticeService;
import com.xuanmiao.smsback.service.notice.bo.BusinessNoticeBO;
import com.xuanmiao.smsback.web.controller.msg.input.BusinessNoticeInputBO;
import com.xuanmiao.smsback.web.controller.msg.query.BusinessNoticeQuery;
import com.xuanmiao.smsback.web.controller.msg.update.BusinessNoticeUpdateBO;
import com.xuanmiao.smsback.web.controller.msg.vo.BusinessNoticeVO;
import com.xuanmiao.smsback.web.controller.msg.vo.ServiceDateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author shenJianKang
 * @date 2019/8/21 20:44
 */

@Api("消息通知")
@RestController
@RequestMapping("/notice")
public class NoticeController {
    private final Logger LOGGER = LoggerFactory.getLogger(getClass());


    @Autowired
    private INoticeService noticeService;


    @ApiOperation(value = "查询商户公告")
    @GetMapping("/queryBusinessNotice")
    public BasicResult<List<BusinessNoticeVO>> listBusinessNotice(BusinessNoticeQuery query){
        try {

            //商家公告列表这边的排序：一级排序未发布>已发布，二级排序发布时间升序；

            BusinessNoticeQO qo = new BusinessNoticeQO();
            if(query != null){
                qo = DozerUtils.convert(query, BusinessNoticeQO.class);
            }
            qo.setStatus(1);
            List<BusinessNoticeBO> boList = noticeService.listBusinessNotice(qo);
            if(CollectionUtils.isNotEmpty(boList)){
                List<BusinessNoticeVO> voList = DozerUtils.convertList(boList, BusinessNoticeVO.class);
                return BasicResult.ok(voList, qo.getPage());
            }
            return BasicResult.ok(Collections.emptyList());
        }catch (Exception e){
            LOGGER.error(e.getMessage(), e);
            if(e instanceof NoticeException){
                return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(),e.getMessage());
            }
            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(),"queryBusinessNotice error");
        }
    }


    @ApiOperation(value = "添加商户公告")
    @PostMapping("/addBusinessNotice")
    public BasicResult<Void> addBusinessNotice(@RequestBody BusinessNoticeInputBO inputBO){
        try {
            BusinessNoticeBO bo = DozerUtils.convert(inputBO, BusinessNoticeBO.class);
            noticeService.addBusinessNotice(bo);
            return BasicResult.ok();
        }catch (Exception e){
            LOGGER.error(e.getMessage(), e);
            if(e instanceof NoticeException){
                return BasicResult.error(BasicResultCode.ADD_FAIL.getCode(),e.getMessage());
            }
            return BasicResult.error(BasicResultCode.ADD_FAIL.getCode(),"addBusinessNotice error");
        }
    }


    @ApiOperation(value = "编辑商户公告")
    @PutMapping("/updateBusinessNotice")
    public BasicResult<Void> updateBusinessNotice(@ApiParam("用户id") @RequestHeader("sms-uid") Integer userId,
                                                  @RequestBody BusinessNoticeUpdateBO updateBO){
        try {
            BusinessNoticeBO bo = DozerUtils.convert(updateBO, BusinessNoticeBO.class);
            // 设置创建人姓名
            EmployeeBO employeeBO = EmployeeUtil.getCurrentEmployee();
            bo.setCreator(employeeBO.getUsername());
            noticeService.updateBusinessNotice(bo);
            return BasicResult.ok();
        }catch (Exception e){
            LOGGER.error(e.getMessage(), e);
            if(e instanceof NoticeException){
                return BasicResult.error(BasicResultCode.UPDATE_FAIL.getCode(),e.getMessage());
            }
            return BasicResult.error(BasicResultCode.UPDATE_FAIL.getCode(),"updateBusinessNotice error");
        }
    }


    @ApiOperation(value = "查询服务器时间")
    @GetMapping("/getServiceDate")
    public BasicResult<ServiceDateVO> getServiceDate(){
        try {
            ServiceDateVO vo = new ServiceDateVO();
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
            vo.setCurrentDate(date);
            vo.setCurrentDateStr(sdf.format(date));
            vo.setCurrentDateTimestamp(date.getTime());

            return BasicResult.ok(vo);
        }catch (Exception e){
            LOGGER.error(e.getMessage(), e);
            return BasicResult.error(BasicResultCode.QUERY_FAIL.getCode(),"getServiceDate error");
        }
    }

    public static void main(String[] args) {
        new NoticeController().getServiceDate();
    }
}
